package com.sky.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.dto.AdminDTO;
import com.sky.entity.Admin;
import com.sky.entity.AdminVO;
import com.sky.result.Result;
import com.sky.service.IAdminService;
import com.sky.utils.InviteCodeGenerator;
import com.sky.utils.SnowflakeIdGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author c_shunyi
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    IAdminService adminService;
    //雪花算法
    @Autowired
    SnowflakeIdGenerator snowflakeIdGenerator;

    @ApiOperation("查询管理员信息")
    @GetMapping("getAdmin/{adminId}")
    Result<AdminVO> getAdmin(@PathVariable("adminId") Long adminId){
        Admin admin = adminService.getOne(new QueryWrapper<Admin>()
                .eq("admin_id", adminId));
        if (admin.getStatus() == 1){
            return Result.error("该用户已禁用");
        }
        AdminVO adminVO = new AdminVO();
        BeanUtil.copyProperties(admin,adminVO);
        return Result.success(adminVO);
    }

    @ApiOperation("添加管理员")
    @PostMapping("addAdmin")
    Result<AdminVO> addAdmin(@RequestBody AdminDTO adminDTO){
        Admin admin = new Admin()
                .setAdminName(adminDTO.getAdminName())
                .setAdminId(snowflakeIdGenerator.nextId())
                .setInvitationCode(InviteCodeGenerator.generateInviteCode());
        if (adminService.save(admin)){
            return Result.success();
        }
        return Result.error("添加失败");
    }
}
